package com.yu.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * 
 * 切面类
 * 
 	@After 通知方法会在目标方法返回或抛出异常后调用
	
	@AfterReturning 通知方法会在目标方法返回后调用
	
	@AfterThrowing 通知方法会在目标方法抛出异常后调用
	
	@Around 通知方法会将目标方法封装起来
	
	@Before 通知方法会在目标方法调用之前执行
 */
@Aspect
@Component
public class AopTest {

	@Pointcut(value = "execution(* com.yu.aop.UserService.goToSchool(..))")
	public void goToSchool(){}
	
	@Pointcut(value = "execution(* com.yu.aop.UserService.markWork(UserModel)) && args(user)")
	public void markWork(UserModel user){}
	
	/**
	 *  && args(user)
	 *  UserModel user
	 */
	
	
	@Pointcut(value = "execution(* com.yu.aop.UserService.goToPaly(UserModel)) && args(user)")
	public void goToPaly(UserModel user){}
	
	/**
	方法之前
	 */
	@Before(value = "goToSchool()")
	public void xishou() {
		System.out.println("这孩子在洗手");
	}
	
	/**
	 * 
	方法之后
	 */
	@After(value = "goToSchool()")
	public void fangxue() {
		System.out.println("孩子放学回家了");
	}

	
//	@Before(value = "markWork()")
//	public void chuanxie() {
//		System.out.println("这孩子在穿鞋");
//	}

	@After(value = "markWork(user)")
	public void zuoxia(UserModel user) {
		user.setName("二狗");
		
		System.out.println("这孩子坐下了");
	}
	
	
	
	/**
	 * 环绕切面
	 */
	@Around(value = "goToPaly(user)")
	public void goToPalyAround(ProceedingJoinPoint pj,UserModel user){
		user.setAge(50);
		try {
			System.out.println("出去玩之前换衣服");
			System.out.println("带好钥匙与手机");
			pj.proceed();
			System.out.println("回来了，洗洗手");
			System.out.println("换衣服");
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	
	

}
